package cn.yj.core.business.controller;

import cn.yj.annotation.pagehelper.page.OrderBy;
import cn.yj.annotation.pagehelper.page.PageUtils;
import cn.yj.cgs.dba.entity.Integral;
import cn.yj.syslog.common.LogType;
import cn.yj.syslog.common.OperateLog;
import cn.yj.config.annotation.Anonymous;
import cn.yj.config.web.AppBaseController;
import cn.yj.core.business.service.IntegralService;
import cn.yj.entity.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author 永健
 * @since 2021-05-07 14:41
 */
@RestController
@RequestMapping("/integral")
public class IntegralController extends AppBaseController<Integral> {

    @Resource
    private IntegralService integralService;


    @GetMapping("/getIntegral")
    public R getIntegral() {
        return success(integralService.getIntegralByUserCode(getUserCode()));
    }

    /**
     * 获取剩余的积分
     */
    @GetMapping("/getIntegralAll")
    public R getIntegralAll() {
        return success(integralService.getSurplusIntegralByUserCode(getUserCode()));
    }

    @GetMapping("/getList")
    @Anonymous
    public R getList(String type) {
        return success(integralService.getListByUserCode(PageUtils.page(new OrderBy(OrderBy.Direction.DESC,
                "create_time")), getUserCode(), type));
    }

    @GetMapping("/progress/{orderNo}")
    public R progress(@PathVariable String orderNo) {
        return success(integralService.getProgress(orderNo));
    }

    /**
     * 每日签到
     */
    @OperateLog(describe = "每日签到",type = LogType.UPDATE)
    @PostMapping("/signIn")
    public R signIn() {
        return integralService.signIn(getUserCode());
    }

    @GetMapping("/getKeepSignDay")
    public R getKeepSignDay() {
        return success(integralService.getSignDay(getUserCode()));
    }

    /**
     * 积分规则说明
     */
    @Anonymous
    @GetMapping("/rules")
    public R rules() {
        return success(integralService.getIntegralRules());
    }


    /**
     * 积分提现
     * @param params  - amount 积分
     */
    @OperateLog(describe = "积分提现",type = LogType.UPDATE)
    @PostMapping("/withdrawal")
    public R withdrawal(@RequestBody Map<String, Object> params) {
        params.put("userCode", getUserCode());
        if (integralService.withdrawal(params)) {
            return success().setMsg("申请已提交,工作人员将会在5个工作日给您转账");
        }
        return error("提现失败，请再试试");
    }


    /**
     * 滚动提示
     */
    @GetMapping("/getRollRecords")
    @Anonymous
    public R getRollRecords(){
        return success(integralService.getRollRecords(PageUtils.page(new OrderBy(OrderBy.Direction.DESC,"i.create_time"))));
    }

}
